<template>
	<view class="payment-container">
		<view class="payment-card">
			<view class="payment-header">
				<text class="payment-title">{{paymentType === 'light' ? '照明电费' : '空调电费'}}缴纳</text>
			</view>

			<view class="payment-info">
				<text class="info-label">寝室号:</text>
				<text class="info-value">{{dorid}}</text>
			</view>

			<view class="payment-info">
				<text class="info-label">当前电费:</text>
				<text :class="'info-value ' + (amount<=0?'red':'')">{{amount}} 元</text>
			</view>

			<view class="amount-input">
				<text class="input-label">缴费金额 (元):</text>
				<input class="input" type="digit" v-model="paymentAmount" placeholder="请输入缴费金额" />
			</view>

			<button class="confirm-btn" @click="handlePayment">确认支付</button>
		</view>
	</view>
</template>

<script>
	import {
		recharge
	} from "@/utils/back.js"
	export default {
		data() {
			return {
				paymentType: '', // light or ac
				amount: 0,
				dorid: '',
				paymentAmount: ''
			}
		},
		onLoad(options) {
			this.dorid = options.dorid
			this.paymentType = options.type || 'light'
			this.amount = parseFloat(options.amount) || 0
			// 默认支付金额为欠费金额
			this.paymentAmount = this.amount.toString()

			// 设置导航栏标题
			uni.setNavigationBarTitle({
				title: this.paymentType === 'light' ? '照明电费缴纳' : '空调电费缴纳'
			})
		},
		methods: {
			goBack() {
				uni.switchTab({
					url: "/pages/message/message"
				})
			},
			async handlePayment() {
				if (!this.paymentAmount || isNaN(this.paymentAmount)) {
					uni.showToast({
						title: '请输入正确的金额',
						icon: 'none'
					})
					return
				}

				const amount = parseFloat(this.paymentAmount)
				if (amount <= 0) {
					uni.showToast({
						title: '金额必须大于0',
						icon: 'none'
					})
					return
				}

				uni.showLoading({
					title: '支付处理中...'
				})

				// 模拟支付过程
				let res = await recharge(this.dorid, (this.paymentType === 'light' ? 1 : 0), amount)

				if (res) {
					uni.showToast({
						title: "操作成功！",
						icon: "success"
					})
				} else {
					uni.showToast({
						title: "操作失败！",
						icon: "error"
					})
				}

				uni.hideLoading()
				uni.navigateBack()
			}
		}
	}
</script>

<style lang="scss">
	.payment-container {
		padding: 30rpx;
		background-color: #f5f5f5;
		min-height: 100vh;
	}

	.payment-card {
		background-color: #fff;
		border-radius: 16rpx;
		padding: 30rpx;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
	}

	.payment-header {
		margin-bottom: 40rpx;
	}

	.payment-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
		position: relative;
		padding-left: 20rpx;

		&::before {
			content: '';
			position: absolute;
			left: 0;
			top: 50%;
			transform: translateY(-50%);
			width: 8rpx;
			height: 36rpx;
			background-color: #07c160;
			border-radius: 4rpx;
		}
	}

	.payment-info {
		display: flex;
		align-items: center;
		margin-bottom: 30rpx;
		font-size: 32rpx;

		.info-label {
			color: #666;
			width: 180rpx;
		}

		.info-value {
			color: #333;
			font-weight: bold;
		}

		.red {
			color: red;
		}
	}

	.amount-input {
		margin: 40rpx 0;

		.input-label {
			display: block;
			font-size: 30rpx;
			color: #666;
			margin-bottom: 20rpx;
		}

		.input {
			height: 90rpx;
			border: 1rpx solid #ddd;
			border-radius: 8rpx;
			padding: 0 20rpx;
			font-size: 32rpx;
		}
	}

	.confirm-btn {
		margin-top: 50rpx;
		height: 90rpx;
		line-height: 90rpx;
		font-size: 32rpx;
		color: #fff;
		background-color: #07c160;
		border-radius: 45rpx;

		&::after {
			border: none;
		}
	}
</style>